package com.promfy.bughunter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.promfy.bughunter.domain.ScoreBoard;
import com.promfy.bughunter.views.GameBackView;
import com.promfy.bughunter.views.GameModeView;
import com.promfy.bughunter.views.GamePlayingView;
import com.promfy.bughunter.views.GameResultsView;

public class BugHunterActivity extends Activity {
	public final static int STATUS_GAME_MODE = 1;
	public final static int STATUS_GAME_PLAYING = 2;
	public final static int STATUS_GAME_RESULTS = 3;
	public final static int STATUS_GAME_BACK = 4;
	public final static int STATUS_GAME_RESUME_PLAYING = 5;
	
	GamePlayingView gamePlayingView;
	GameModeView gameModeView;
	GameBackView gameBackView;
	GameResultsView gameResultsView;
	private FrameLayout frame;
	private int status;
	private Intent intent;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.main);
		frame = (FrameLayout) findViewById(R.id.frame);
		
//		gamePlayingView = new GamePlayingView(this);
//        setContentView(gamePlayingView);
//		gameResultsView = new GameResultsView(this);
//		setContentView(gameResultsView);
		gameModeView = new GameModeView(this);
		frame.addView(gameModeView);
//		intent = new Intent("com.angel.Android.MUSIC");
//		startService(intent);

//		frame.addView(gameBackView);
    }
      
    public void onState(final int status){
    	this.status = status;
    	//runOnUiThread(new Runnable() {
			
			//@Override
			//public void run() {
				// TODO Auto-generated method stub				
				switch (status) {
				case STATUS_GAME_MODE:
					if(gamePlayingView!=null) gamePlayingView.setExsit(false);
					gameModeView = GameModeView.getInstance(BugHunterActivity.this);
					frame.removeAllViews();
					frame.addView(gameModeView);
					break;
				case STATUS_GAME_PLAYING:
					if(gamePlayingView!=null) gamePlayingView.setExsit(false);
					gamePlayingView = GamePlayingView.getNewInstance(BugHunterActivity.this);
					frame.removeAllViews();
					frame.addView(gamePlayingView); 
					gameBackView = new GameBackView(BugHunterActivity.this);
					gameBackView.setVisibility(View.INVISIBLE);
					frame.addView(gameBackView);
					break;
				case STATUS_GAME_RESULTS:
					if(gamePlayingView!=null) gamePlayingView.setExsit(false);
					gameResultsView = new GameResultsView(BugHunterActivity.this);
					frame.removeAllViews();
					frame.addView(gameResultsView);  
					gameResultsView.bringToFront();
					break;
				case STATUS_GAME_BACK:
					gameBackView.setVisibility(View.VISIBLE);
					gameBackView.bringToFront();
					gameBackView.postInvalidate();
					gamePlayingView.setPause(true)  ; 
					break;
				case STATUS_GAME_RESUME_PLAYING:
					gamePlayingView = GamePlayingView.getResumeInstance(BugHunterActivity.this);
					gameBackView.setVisibility(View.INVISIBLE);
					break;
				default:
					break;
				}
			//}
		//});
				

    }
    
    @Override
    protected void onStop() {
    	// TODO Auto-generated method stub
    	super.onStop();
//    	stopService(intent);
		if(gamePlayingView!=null) gamePlayingView.setExsit(false);
    	finish();
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 
           if(status == STATUS_GAME_PLAYING){
        	   onState(STATUS_GAME_BACK);
           }else if (status == STATUS_GAME_BACK){
        	   onState(STATUS_GAME_RESUME_PLAYING);
           }else if (status == STATUS_GAME_RESUME_PLAYING){
        	   onState(STATUS_GAME_BACK);
           }else{        	   
        	   return super.onKeyDown(keyCode, event);
           }
        	return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    
    
}